/**
 * 
 */
package l1j.server.server.model.Instance;




import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Object;
import l1j.server.server.serverpackets.S_DummyPack;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_RemoveObject;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.templates.L1Npc;

/**
 * @author user
 *
 */
public class L1DummyInstance extends L1NpcInstance
{
	private static final long serialVersionUID = 1L;

	private String shopName;

	/**
	 * @param template
	 */
	public L1DummyInstance(L1Npc template)
	{
		super(template);

		//		if (!(template.get_npcId() >= 97302 && template.get_npcId() <= 97313))
		//		{
		//			L1World.getInstance().getDummyList().add(this);
		//			L1World.getInstance().addDummyAmount();
		//		}
	}

	public void setShopName(String shopName)
	{
		this.shopName = shopName;
	}

	public String getShopName()
	{
		return shopName;
	}

	//@Override
	@Override
	public void onPerceive(L1PcInstance perceivedFrom)
	{
		perceivedFrom.addKnownObject(this);
		perceivedFrom.sendPackets(new S_DummyPack(this));
	}

	//@Override
	@Override
	public void onTalkAction(L1PcInstance pc)
	{
		pc.sendPackets(new S_NPCTalkReturn(this));
	}


	private int _levelment;

	public void setLevelMent(final int i){
		_levelment = i;
	}

	public int getLevelMent(){
		return _levelment;
	}

	private int _AI_TYPE = 0;


	@Override
	public void searchTarget() {
		// 攻擊目標搜尋
		L1Character target = null;
		//	L1MonsterInstance target = null;
		//		final L1Npc Npc = this.getNpcTemplate();
		final int mapid = this.getMapId();

		for (final L1Object obj : L1World.getInstance().getVisibleObjects(mapid).values()) {
			if(_AI_TYPE == 0){	
				if(this.getMapId() != 1784){
					this.teleport(32773, 32844, 5 , (short) 1784);
				}
				if( obj instanceof L1MerchantInstance && ((L1MerchantInstance) obj).getNpcId() == 710025){
					target = (L1Character) obj;
					break;
				}
			}else if(_AI_TYPE == 1){
				if( obj instanceof L1DwarfInstance && ((L1DwarfInstance) obj).getNpcId() == 60024){
					target = (L1Character) obj;
					break;
				}
			}else if (_AI_TYPE == 2){
				_AI_TYPE = 3;
				this.teleport(32733, 32790, 5 , (short) 370);
				break;
			}else{
				if( obj instanceof L1PcInstance){
					final L1PcInstance pc = (L1PcInstance)obj;

					if (pc.getCurrentHp() <= 0) {
						continue;
					}
					if (pc.isDead()) {
						continue;
					}
					if (pc.isGhost()) {
						continue;
					}
					if (pc.isMonitor()) {
						continue;
					}
					if (pc.isGm()) {
						continue;
					}
					if (pc.isTeleport()){
						continue;
					}

				}
			}
		}
		if (target != null) {
			_hateList.add(target, 0);
			_target = target;
		}
	}

	@Override
	public void receiveDamage(final L1Character attacker,int damage) { // 攻撃でＨＰを減らすときはここを使用
		if(damage >0){
			if(this.getMap().isTeleportable()){
				randomTeleport();
			}
		}
	}

	/**
	 * 隨機執行移動
	 * @param pc
	 * @param effectable
	 */
	private void randomTeleport() {
		try {
			// まだ本サーバのランテレ処理と違うところが結構あるような・・・
			final L1Location newLocation = this.getLocation().randomLocation(200, true);
			final int newX = newLocation.getX();
			final int newY = newLocation.getY();
			this.teleport(newX, newY, 5);
		} catch (final Exception e) {
			//			_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
		}
	}

	/**
	 * 目標位置指定標傳送
	 * @param nx
	 * @param ny
	 * @param dir
	 */
	private void teleport(final int nx, final int ny,final int dir, final short mapid) {
		try {
			this.getMap().setPassable(this.getX(), this.getY(),true);
			for (final L1PcInstance pc : L1World.getInstance().getRecognizePlayer(this)) {
				pc.sendPackets(new S_SkillSound(this.getId(), 169));
				pc.sendPackets(new S_RemoveObject(this));
				pc.removeKnownObject(this);
			}
			this.setMap(mapid);
			this.setX(nx);
			this.setY(ny);
			this.setHeading(dir);
			this.getMap().setPassable(nx, ny,false);
			L1World.getInstance().addVisibleObject(this);
		} catch (final Exception e) {
		}
	}

	@Override
	public void onNpcAI() {
		if (isAiRunning()) {
			return;
		}
		setActived(false);
		startAI();
	}

	// ターゲットがいる場合の処理
	@Override
	public void onTarget() {
		this.setActived(true);
		this._targetItemList.clear();
		this._targetItem = null;
		final L1Character target = this._target; // ここから先は_targetが変わると影響出るので別領域に参照確保
		if (this.isAttackPosition(target.getX(), target.getY(), this.getNpcTemplate().get_ranged())) { // 攻撃可能位置
			this.setHeading(this.targetDirection(target.getX(), target.getY()));
			this.attackTarget(target);
			if(_AI_TYPE ==0){
				_AI_TYPE = 1;
				this.tagertClear();
				this.setSleepTime(5000);
				try {
					Thread.sleep(5000);

				} catch (final Exception e) {
				}
			}else if(_AI_TYPE == 1){
				this.setSleepTime(15000);
				_AI_TYPE = 2;
				this.tagertClear();
				try {
					Thread.sleep(15000);

				} catch (final Exception e) {
				}
			}
			if (this._npcMove != null) {
				this._npcMove.clear();
			}
		} else { // 攻撃不可能位置

			if (this.getPassispeed() > 0) {
				// 移動できるキャラ
				final int distance = this.getLocation().getTileDistance(target.getLocation());
				if(distance<=4){
					if(_AI_TYPE ==0 ){
						this.setSleepTime(5000);
						_AI_TYPE = 1;
					}else if(_AI_TYPE == 1){
						this.setSleepTime( 15000);
						_AI_TYPE = 2;
					}
					this.tagertClear();	
					try {
						Thread.sleep(this.getSleepTime());

					} catch (final Exception e) {
					}
				}
				//						if (this.getNpcTemplate().is_teleport()&& 20 > Random.nextInt(100)
				//								&& this.getCurrentMp() >= 10 && distance > 6
				//								&& distance < 15) { // テレポート移動
				//							if (this.nearTeleport(target.getX(), target.getY()) == true) {
				//								return;
				//							}
				//						}

				if (this._npcMove != null) {
					final int dir = this._npcMove.moveDirection(target.getX(), target.getY());
					if (dir == -1) {
						this.tagertClear();

					} else {
						this._npcMove.setDirectionMove(dir);
						this.setSleepTime(this.calcSleepTime(this.getPassispeed(), MOVE_SPEED));
					}
				}
			} else {
				// 移動できないキャラ（ターゲットから排除、ＰＴのときドロップチャンスがリセットされるけどまぁ自業自得）
				this.tagertClear();
			}
		}
	}
}
